var area = require('../../utils/area.js');
const request = require('../../utils/https.js')
import Page from '../../common/page';

let userId;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        areaList: area.default,
        checked: false,
        isDefault: 0,
        wxAddress: false//微信地址按钮
    },

  //输出 省 市 区
  confirm(event) {
      console.log('选择城市====>', event)
      let town = event.detail.values[2];
      if (town != null && town != undefined) {
          town = town.name;
      } else {
          town = "";
      }
      //区县;
      this.setData({
          show: false,
          code: event.detail.values[0].code, //省级编码
          province: event.detail.values[0].name, //省
          city: event.detail.values[1].name, //城市
          town: town, //区县
          areAdderss: true,
          address: event.detail.values[0].name + ' ' + event.detail.values[1].name + ' ' + town

      })
    },

    cancel() {
        this.setData({
            show: false
        })
    },
    getCity() {
        this.setData({
            show: true
        })
    },

    onClose() {
        this.setData({
            show: false
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        userId = wx.getStorageSync('userId');
        //addressId有值为编辑 空值为新增

        let addressId = options.addressId;

        let oneAdd = options.add

        if (oneAdd == 'oneAdd') { //第一次添加默认开启
            that.setData({
                checked: true,
                isDefault: 1,
                wxAddress: true
            })
        }
        that.setData({
            addressId: addressId
        })


        if (addressId != ' ') {
            //编辑地址 回显
            console.log('编辑地址====》')
            request._get(getApp().Api_url + 'userAddress/mine', { //地址列表接口
                userId: userId,
                type: 1,
                tradeType: -1

            }, function (res) {
                console.log('地址回显===>', res)
                if (res.data.errCode == 200) {
                    let addressList = res.data.data;
                    addressList.forEach((v) => {
                        //查出默认地址
                        if (addressId == v.addressId) {
                            if (v.isDefault == 1) {
                                var checked = true;
                            } else {
                                var checked = false;
                            }
                            that.setData({
                                adderssData: v,
                                address: v.province + ' ' + v.city + ' ' + v.town,
                                checked: checked,
                                isDefault: v.isDefault,
                                code: v.code,
                                province: v.province,
                                city: v.city,
                                town: v.town,
                                others: v.others,
                                /* postal: v.postal, */
                                username: v.userName,
                                mobile: v.mobile

                            })
                        }

                    });

                }
            }, function () {
            });
        } else {
            console.log('新增地址===>')
        }

        //地址列表查询

    },
    getUserName(e) {
        const that = this;
        let userName = e.detail.detail.value;
        that.setData({
            username: userName
        })
    },

    getPhone(e) {
        const that = this;
        let mobile = e.detail.detail.value;
        that.setData({
            mobile: mobile
        })
    },
    //验证手机号// 获取电话
    testPhone: function (e) {
        var phone = e.detail.detail.value;
        const that = this;
        if (that.isPoneAvailable(phone)) {
            that.setData({
                testPhone: true
            })
        } else {
            wx.showToast({
                title: '手机号有误，请重新输入！',
                icon: 'none',
                duration: 2000
            })
            that.setData({
                testPhone: false
            })
        }
    },

    // 验证手机号2 11位
    isPoneAvailable(str) {
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/;
        if (!myreg.test(str)) {
            return false;
        } else {
            return true;
        }
    },


    //详细地址
    getOthers(e) {
        const that = this;
        let others = e.detail.detail.value;
        that.setData({
            others: others
        })
    },
    /* getCode(e){
      const that = this;
      let postal = e.detail.detail.value;
      that.setData({
        postal: postal
      })
    }, */


    //是否设为常用地址 开关
    onChange(event) {
        const that = this;
        // 需要手动对 checked 状态进行更新
        if (event.detail) {
            that.setData({
                checked: event.detail,
                isDefault: 1
            });
        } else {
            that.setData({
                checked: event.detail,
                isDefault: 0
            });
        }
    },

    postSubmit() {
        const that = this;
        var code = that.data.code;
        var province = that.data.province;
        var city = that.data.city;
        var town = that.data.town;
        var others = that.data.others;
        /* var postal = that.data.postal; */
        var mobile = that.data.mobile;
        var username = that.data.username
        var testPhone = that.data.testPhone
        var isDefault = that.data.isDefault;

        console.log('isDefault===>', isDefault)


        if (that.data.addressId != ' ') {
            console.log('编辑')
            var addressId = that.data.addressId;
        } else {
            console.log('新增')
            var addressId = '';
        }
        console.log('code==>', code)
        console.log('province==>', province)
        console.log('city==>', city)
        console.log('town==>', town)
        console.log('others==>', others)
        /* console.log('postal==>', postal) */
        console.log('mobile==>', mobile)
        console.log('username==>', username)
        console.log('testPhone==>', testPhone)
        console.log('isDefault==>', isDefault)

        //判断信息填写完整没有
        if (that.isPoneAvailable(mobile)) {
            if (province && others && username && mobile /* && postal */) {
                request._post(getApp().Api_url + 'userAddress/add', { //添加地址
                    userId: userId,
                    userName: username,
                    code: code,
                    province: province,
                    city: city,
                    town: town,
                    others: others,
                    /* postal: postal, */
                    mobile: mobile,
                    isDefault: isDefault,
                    addressId: addressId
                }, function (res) {

                    console.log('提交地址===>', res)

                    if (res.data.errCode == 200) {
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        wx.showToast({
                            title: '提交失败',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                }, function () {
                })
            } else {
                wx.showToast({
                    title: '信息不完整！',
                    icon: 'none',
                    duration: 2000
                })
            }
        } else {
            wx.showToast({
                title: '手机号有误，请重新输入！',
                icon: 'none',
                duration: 2000
            })

        }
    },
    chooseAddress() {
        const that = this;
        wx.chooseAddress({
            success: (res) => {

                if (res.errMsg == 'chooseAddress:ok') {
                    console.log('res===>', res)
                    let provinceName = res.provinceName;
                    let cityName = res.cityName;
                    let countyName = res.countyName;
                    let detailInfo = res.detailInfo;
                    let nationalCode = (res.nationalCode).substring(0, 2) + '0000';
                    let userName = res.userName;
                    let telNumber = res.telNumber;
                    console.log('nationalCode==>', nationalCode)
                    that.postFrom(provinceName, cityName, countyName, detailInfo, nationalCode, userName, telNumber)
                }


            },
            fail: function (err) {
                console.log(err)
            }
        })
    },
    postFrom(province, city, town, others, code, username, mobile) {
        const that = this;
        var isDefault = 1;
        var addressId = '';
        console.log('code==>', code)
        console.log('province==>', province)
        console.log('city==>', city)
        console.log('town==>', town)
        console.log('others==>', others)
        console.log('mobile==>', mobile)
        console.log('username==>', username)
        console.log('isDefault==>', isDefault)


        /* that.data.addressList.forEach(v => {
          if (code == v.code && province === v.province && city === v.city && town === v.town && others === v.others && username === v.userName && mobile === v.mobile) {
            console.log('编辑')
            addressId = v.addressId;
          } else {
            console.log('新增')
            addressId = '';
          }
        }); */


        //判断信息填写完整没有
        request._post(getApp().Api_url + 'userAddress/add', { //添加地址
            userId: userId,
            userName: username,
            code: code,
            province: province,
            city: city,
            town: town,
            others: others,
            mobile: mobile,
            isDefault: isDefault,
            addressId: addressId
        }, function (res) {

            console.log('提交地址===>', res)

            if (res.data.errCode == 200) {
                wx.navigateBack({
                    delta: 1
                })
            } else {
                wx.showToast({
                    title: '提交失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        }, function () {
        })


    }

})